package com.augurit.smartform.converter;

import java.util.ArrayList;
import java.util.List;

import com.augurit.smartform.entity.SmComponentRegister;
import com.augurit.smartform.mgmt.SmartComponentTreeNode;
import com.augurit.smartform.web.component.form.ComponentRegisterForm;

public class ComponentRegisterConverter {
	public static ComponentRegisterForm convertToForm(SmComponentRegister entity){
		if(entity!=null){
			ComponentRegisterForm form=new ComponentRegisterForm();
			form.setId(entity.getId());
			form.setName(entity.getName());
			form.setType(entity.getType());
			form.setContent(entity.getContent());
			form.setAvailable(entity.getAvailable());
			return form;
		}else{
			return null;
		}
	}
	
	public static List<ComponentRegisterForm> convertToFormList(List<SmComponentRegister> list){
		if(list!=null&&list.size()>0){
			List<ComponentRegisterForm> result=new ArrayList<ComponentRegisterForm>();
			for(SmComponentRegister entity : list){
				ComponentRegisterForm form=convertToForm(entity);
				result.add(form);
			}
			return result;
		}else{
			return null;
		}
	}
	
	public static SmartComponentTreeNode convertComponentRegisterToTreeNode(ComponentRegisterForm form){
		if(form!=null){
			SmartComponentTreeNode node=new SmartComponentTreeNode();
			node.setId(SmartComponentTreeNode.getKey(SmComponentRegister.class.getSimpleName(), form.getId()));
			node.setText(form.getName());
			node.setQtip(form.getName());
			if("0".equals(form.getAvailable())){
				node.setIconCls(SmartComponentTreeNode.CLS_DELETE);
			}else{
				node.setIconCls(SmartComponentTreeNode.CLS_MODULE);
			}
			node.setLeaf(false);
			return node;
		}else{
			return null;
		}
	}
	
	/**
	 * 将Form对象列表转换为ExtTreeNode对象列表
	 * @param list
	 * @return
	 */
	public static List<SmartComponentTreeNode> convertComponentRegisterToTreeNode(List<ComponentRegisterForm> list){
		List<SmartComponentTreeNode> result = new ArrayList<SmartComponentTreeNode>();
		if(list != null && list.size() > 0){
			for(ComponentRegisterForm form : list)
				result.add(ComponentRegisterConverter.convertComponentRegisterToTreeNode(form));
		}
		return result;
	}
	
	public static void convertToEntity(ComponentRegisterForm form,SmComponentRegister entity){
		if(form!=null&&entity!=null){
			entity.setName(form.getName());
			entity.setType(form.getType());
			entity.setContent(form.getContent());
			entity.setAvailable(form.getAvailable());
		}
	}
}
